﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;

namespace DTS_Helper.Utils
{
    public class TransmitHelper
    {
        private HttpClient httpClient;
        private string dbName;

        public TransmitHelper()
        {
            httpClient = new HttpClient()
            {
                BaseAddress = new Uri("http://zjnewtuwei.com:9208/api/")
            };
            dbName = "fj-zz";
        }

        /// <summary>
        /// 自定义的转发功能
        /// </summary>
        /// <param name="api"></param>
        /// <param name="dataname"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public Task<HttpResponseMessage> Post(string api, object value)
        {
            //new StringContent()
            return httpClient.PostAsync(api + $"?dataname={dbName}", new StringContent(JsonConvert.SerializeObject(value), Encoding.UTF8, "application/json"));
        }

        /// <summary>
        /// 自定义的转发功能
        /// </summary>
        /// <param name="api"></param>
        /// <param name="dataname"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public Task<HttpResponseMessage> PostNormal(string api, object value)
        {
            //new StringContent()
            return httpClient.PostAsync(api, new StringContent(JsonConvert.SerializeObject(value), Encoding.UTF8, "application/json"));
        }

        public static async Task<HttpResponseMessage> MyHttpGet(string baseUrl, Dictionary<string, string> param)
        {
            HttpClient client = new HttpClient();
            string getUrl = baseUrl;
            if (param.Count == 0)
            {
                return await client.GetAsync(getUrl);
            }
            else
            {
                string paramStr = "";
                var list = param.ToList();
                for (var i = 0; i < list.Count; i++)
                {

                    if (i != 0)
                    {
                        paramStr += "&";
                    }
                    paramStr += $"{list[i].Key}={list[i].Value}";
                }
                getUrl += "?" + paramStr;
                return await client.GetAsync(getUrl);
            }
        }

        public async Task<bool> GetAlarmStatus(string TermianlId)
        {
            var res = await MyHttpGet("http://zjnewtuwei.com:9208/api/Alarm/GetAlarmFlag", new Dictionary<string, string>()
                    {
                        {"TerminalId", TermianlId}
                    });
            return await res.Content.ReadFromJsonAsync<bool>();
        }

        public async Task SendAlarm(string TermianlId, decimal Value, string Content,string code = "509")
        {
            await MyHttpGet("http://zjnewtuwei.com:9208/api/Alarm/InsertAlarmValue", new Dictionary<string, string>()
            {
                {"TerminalId",TermianlId },
                {"AlarmCode",code },
                {"Value",Value.ToString()},
                {"Content",Content }
            });
        }

        public async Task SendPhoneMsg(string Msg)
        {
            var phoneList = new List<string>() { "13799049417", "13489666811", "18059687618", "13605066161", "13159150860" };
            //var phoneList = new List<string>() { "18858782809" };

            for (var i = 0; i < phoneList.Count; i++)
            {
                await MyHttpGet("http://zjnewtuwei.com:9208/api/Alarm/SendPhoneMsg", new Dictionary<string, string>() {
                {"phone", phoneList[i]},
                {"msg",Msg }
            });
            }
            //var Phone = "18858782809";

        }

        public async Task CancelAlarm(string TerminalId)
        {
            await MyHttpGet("http://zjnewtuwei.com:9208/api/Alarm/CancelAlarm", new Dictionary<string, string>() {
                {"TermianlId", TerminalId}
            });
        }
    }
}
